﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using System.Threading;
using bll;
using System.Drawing;

namespace TimeIt.beispiele
{
    public partial class management : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            bll.clsUserCollection _UserCol = new bll.clsUserCollection();
            bll.clsUser _clsIdUser = new bll.clsUser();
            Int32 sessionId = bll.clsUser.Session;
            if (sessionId == 0) { Response.Redirect("login.aspx"); }
            _clsIdUser = _UserCol.GetUserNameBySessionId();
            UserName.Text = _clsIdUser.Title + " " + _clsIdUser.Name;

            // Methode CountUsers2() aufrufen
            lblCount2.Text = Convert.ToString(_UserCol.CountUsers1());

            //bll.clsUser pl = _UserCol.GetUserByPosition("Projectleader");
            //grdPL.Attributes.AddAttributes(pl);
        }

        /// <summary>
        /// wird bei Klick des Einfügen-Knopfs aufgerufen
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void insert_btn_click(object sender, System.EventArgs e)
        {
            bll.clsProject newProject = new bll.clsProject();
            bll.clsProjectCollection myProjectCol = new bll.clsProjectCollection();

            newProject.Name = Convert.ToString(txtPName.Text);
            newProject.Client = Convert.ToString(txtPClient.Text);
            newProject.ContractPrice = Convert.ToInt32(txtPPreis.Text);
            newProject.ExternalHourlyRate = Convert.ToInt32(txtPHourlyrate.Text);
            newProject.Active = check_active.Checked;
            newProject.ContactPerson = Convert.ToString(txtPContactPerson.Text);
            newProject.Address = Convert.ToString(txtPAddress.Text);
            newProject.Postcode = Convert.ToString(txtPPostCode.Text);
            newProject.Location = Convert.ToString(txtPLocation.Text);
            newProject.Country = Convert.ToString(txtPCountry.Text);
            newProject.TelNr = Convert.ToString(txtPTelNr.Text);

            bll.clsUser newCustomer = new bll.clsUser();
            bll.clsUserCollection newCustomerCol = new bll.clsUserCollection();

            newCustomer.Name = Convert.ToString(txtPContactPerson.Text);
            newCustomer.Title = Convert.ToString(DropDownTitle.Text);
            newCustomer.Email = Convert.ToString(txtPEmail.Text);
            newCustomer.Position = "Kunde";
            newCustomer.LoginPw = "admin";
            newCustomer.InternalHourlyRate = 0;

            if (myProjectCol.InsertProject(newProject) == 1 && newCustomerCol.InsertUser(newCustomer) == 1)
            {
                checkPMsg.Text = "Adding the project: "+ newProject.Name +" successfully!";
                grdProject.DataBind();
            }
            else
            {
                checkPMsg.Text = "Adding the project: "+ newProject.Name +" failed!";
            }
        } //insert_btn()

        protected void manageMenu_MenuItemClick(object sender, MenuEventArgs e)
        {
            if (manageMenu.SelectedValue.Equals("first"))
                MultiView1.ActiveViewIndex = 0;
            if (manageMenu.SelectedValue.Equals("second"))
                MultiView1.ActiveViewIndex = 1;
            if (manageMenu.SelectedValue.Equals("third"))
                MultiView1.ActiveViewIndex = 2;
            if (manageMenu.SelectedValue.Equals("fourth"))
                MultiView1.ActiveViewIndex = 3;
        }

        protected void btninsert_projectleader(object sender, EventArgs e)
        {
            checkPMsg.Text = ""; errorMsgName.Text = ""; errorMsgEmail.Text = "";
            errorMsgHourlyRate.Text = ""; ErrorMsgPW.Text = "";

            String[] msgFilled = new String[5];
            functionAdd add = new functionAdd();
            msgFilled = add.addNewEmployee(Convert.ToString(DropDownTitle.Text),
                                           txtUName.Text,
                                           "Projectleader",
                                           txtUHourlyrate.Text,
                                           txtUPw.Text,
                                           txtUEmail.Text);

            checkPMsg.Text = msgFilled[0];
            checkPMsg.ForeColor = Color.FromArgb(Convert.ToInt32(msgFilled[1]));
            errorMsgName.Text = msgFilled[2];
            errorMsgEmail.Text = msgFilled[3];
            errorMsgHourlyRate.Text = msgFilled[4];
            ErrorMsgPW.Text = msgFilled[5];

            grdPL.DataBind();
        }
    }
}